<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page isELIgnored="false" %>
<html>
<head>
    <title>员工列表</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/css/style.css">
    <style>
        .pagination {
            margin-top: 15px;
            text-align: center;
        }
        .pagination a, .pagination span {
            display: inline-block;
            padding: 6px 12px;
            margin: 0 4px;
            border: 1px solid #ccc;
            text-decoration: none;
            color: #007bff;
        }
        .pagination span {
            background-color: #007bff;
            color: white;
            border-color: #007bff;
        }
        .search-form {
            margin-bottom: 20px;
        }
        .search-form label {
            margin-right: 10px;
        }
        .search-form input {
            padding: 4px;
            margin-right: 8px;
        }
        .search-form button {
            padding: 4px 10px;
            background-color: #28a745;
            color: white;
            border: none;
            cursor: pointer;
        }
        .search-form a {
            margin-left: 10px;
            color: #dc3545;
            text-decoration: none;
        }
    </style>
</head>
<body>
<div class="container">
    <h2>员工列表</h2>

    <!-- 查询表单 -->
    <c:url var="searchAction" value="/employee/list" />
    <form method="get" action="${searchAction}" class="search-form">
        <div style="display: flex; flex-wrap: wrap; gap: 10px; align-items: center;">
            <!-- 姓名、部门、职位 -->
            <div>
                <label>姓名:
                    <input type="text" name="name" value="${param.name}" style="width: 100px;">
                </label>
            </div>
            <div>
                <label>部门:
                    <input type="text" name="department" value="${param.department}" style="width: 100px;">
                </label>
            </div>
            <div>
                <label>职位:
                    <input type="text" name="position" value="${param.position}" style="width: 100px;">
                </label>
            </div>

            <!-- 入职日期 -->
            <div>
                <label>入职从:
                    <input type="date" name="hireDateFrom" value="${param.hireDateFrom}">
                </label>
            </div>
            <div>
                <label>到:
                    <input type="date" name="hireDateTo" value="${param.hireDateTo}">
                </label>
            </div>

            <!-- 按钮 -->
            <div>
                <button type="submit">查询</button>
                <a href="${searchAction}">清空</a>
            </div>
        </div>
    </form>

    <!-- 新增按钮 -->
    <c:url var="addUrl" value="/employee/add" />
    <a href="${addUrl}" class="btn-add">新增员工</a>

    <!-- 员工表格 -->
    <table border="1" class="employee-table">
        <thead>
        <tr>
            <th>ID</th>
            <th>姓名</th>
            <th>部门</th>
            <th>职位</th>
            <th>薪资</th>
            <th>邮箱</th>
            <th>电话</th>
            <th>入职日期</th>
            <th>创建人</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        <c:forEach var="emp" items="${employees}">
            <tr>
                <td>${emp.id}</td>
                <td>${emp.name}</td>
                <td>${emp.department}</td>
                <td>${emp.position}</td>
                <td>${emp.salary}</td>
                <td>${emp.email}</td>
                <td>${emp.phone}</td>
                <td>${emp.hireDate}</td>
                <td>${emp.createdBy.username}</td>
                <td>
                    <!-- 查看 -->
                    <c:url var="viewUrl" value="/employee/view">
                        <c:param name="id" value="${emp.id}" />
                    </c:url>
                    <a href="${viewUrl}">查看</a> |

                    <!-- 编辑 -->
                    <c:url var="editUrl" value="/employee/edit">
                        <c:param name="id" value="${emp.id}" />
                    </c:url>
                    <a href="${editUrl}">编辑</a> |

                    <!-- 删除 -->
                    <c:url var="deleteUrl" value="/employee/delete">
                        <c:param name="id" value="${emp.id}" />
                    </c:url>
                    <a href="${deleteUrl}" onclick="return confirm('确定删除吗？')">删除</a>
                </td>
            </tr>
        </c:forEach>
        </tbody>
    </table>

    <!-- 分页控件 -->
    <div class="pagination">
        <!-- 上一页 -->
        <c:if test="${currentPage > 1}">
            <c:url var="prevUrl" value="/employee/list">
                <c:param name="page" value="${currentPage - 1}" />
                <c:param name="name" value="${param.name}" />
                <c:param name="department" value="${param.department}" />
                <c:param name="position" value="${param.position}" />
                <c:param name="hireDateFrom" value="${param.hireDateFrom}" />
                <c:param name="hireDateTo" value="${param.hireDateTo}" />
            </c:url>
            <a href="${prevUrl}">上一页</a>
        </c:if>

        <!-- 页码 -->
        <c:forEach begin="1" end="${totalPages}" var="i">
            <c:url var="pageUrl" value="/employee/list">
                <c:param name="page" value="${i}" />
                <c:param name="name" value="${param.name}" />
                <c:param name="department" value="${param.department}" />
                <c:param name="position" value="${param.position}" />
                <c:param name="hireDateFrom" value="${param.hireDateFrom}" />
                <c:param name="hireDateTo" value="${param.hireDateTo}" />
            </c:url>
            <c:choose>
                <c:when test="${currentPage eq i}">
                    <span>${i}</span>
                </c:when>
                <c:otherwise>
                    <a href="${pageUrl}">${i}</a>
                </c:otherwise>
            </c:choose>
        </c:forEach>

        <!-- 下一页 -->
        <c:if test="${currentPage lt totalPages}">
            <c:url var="nextUrl" value="/employee/list">
                <c:param name="page" value="${currentPage + 1}" />
                <c:param name="name" value="${param.name}" />
                <c:param name="department" value="${param.department}" />
                <c:param name="position" value="${param.position}" />
                <c:param name="hireDateFrom" value="${param.hireDateFrom}" />
                <c:param name="hireDateTo" value="${param.hireDateTo}" />
            </c:url>
            <a href="${nextUrl}">下一页</a>
        </c:if>
    </div>

    <!-- 返回首页 -->
    <p><a href="${pageContext.request.contextPath}/dashboard">返回首页</a></p>
</div>
</body>
</html>